/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myftp.app.action;

import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JTable;
import myftp.app.model.ServerManagerModel;
import myftp.app.model.ServerModel;
import myftp.app.model.ServerModel.TransferState;
import myftp.app.model.ServerTransferModel;
import myftp.app.model.TransfertTableModel;
import myftp.app.view.TransfertView;
import myftp.app.view.dialog.TemplateDialog;
import myftp.core.Template;
import myftp.core.action.AbstractAction;
import myftp.core.controller.FrontController;

/**
 *
 * @author CAVARLE Glenn, LUCAS Stéphane
 * Effectue un téléchargment
 */
public class DownloadAction extends AbstractAction {

    public synchronized void run() {
        ServerTransferModel transfer = null;
        ServerModel transferServer = null;

        /* Récupération de différents paramètres */
        String filename = (String) this.getParam("filename");
        String path = (String) this.getParam("path");
        String filePath = path + filename;
        ServerModel aServer = (ServerModel) this.getParam("server");

        /* Demande la confirmation du téléchargement */
        Integer response = TemplateDialog.showDownloadDialog(filename);
        if (response == TemplateDialog.YES_OPTION) {

            ServerManagerModel manager = (ServerManagerModel) this.getModel();
            JTable table = ((TransfertView) Template.getInstance().
                    getViewPart("transfertView")).getTransfertTable();
            TransfertTableModel aTransfertsTable = (TransfertTableModel) table.getModel();


            try {
                if ((transfer = manager.getTransferAction(filePath)) == null) {
                    transfer = new ServerTransferModel(aServer, aTransfertsTable);
                    aTransfertsTable.addTransfert(
                            transfer.getParentServer().getInetAddress().getHostName(),
                            filename);
                }
                transferServer = transfer.getTransferServer();
                if (transferServer.isLoggedIn()) {

                    transferServer.hasTransfer(true);
                    manager.addTransferAction(
                            filePath, transfer);

                    FrontController front;
                    front = FrontController.getInstance();
                    front.setActionParam("server", transferServer);
                    front.setActionParam("table", aTransfertsTable);
                    front.dispatch("TransferPromptAction");

                    transfer.download(path, filename);

                    TransferState state = transferServer.getStateTransfer();

                    if (state.isPaused()) {
                        aTransfertsTable.pauseTransfert(transferServer.getConnectIndex());
                        transferServer.hasTransfer(false);
                    } else if (state.isStoped()) {
                        aTransfertsTable.stopTransfert(transferServer.getConnectIndex());
                        transferServer.hasTransfer(false);
                        manager.deleteTransferAction(filePath);
                    } else {
                        aTransfertsTable.closeTransfert(transferServer.getConnectIndex());
                        transferServer.hasTransfer(false);
                        manager.deleteTransferAction(filePath);
                    }

                }

                /* En cas de problèmes */
            } catch (FileNotFoundException ex) {
                aTransfertsTable.stopTransfert(transferServer.getConnectIndex());
                transfer.deleteBrokenFile(filename);
                transferServer.hasTransfer(false);
                manager.deleteTransferAction(filePath);
                TemplateDialog.showInternalErrorDialog(ex.getLocalizedMessage());
            } catch (IOException ex) {
                aTransfertsTable.stopTransfert(transferServer.getConnectIndex());
                transfer.deleteBrokenFile(filename);
                transferServer.hasTransfer(false);
                manager.deleteTransferAction(filePath);
                TemplateDialog.showInternalErrorDialog(ex.getLocalizedMessage());
            }

            System.out.println("download end");
        }
    }

    public boolean init() {
        return true;
    }
}
